﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TaoPortal.Common.Utility
{
    public class HttpContextCache
    {
        #region 添加缓存
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public static void Add(string key, object value)
        {
            HttpContext.Current.Cache.Add(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
        }
        #endregion

        #region 获取缓存
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static object Get(string key)
        {
            return HttpContext.Current.Cache.Get(key);
        }
        #endregion

        #region 初始化直列表缓存
        /// <summary>
        /// 初始化直列表缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="list"></param>
        public static void InitParamValueCache(string key, Dictionary<string, Dictionary<string, string>> list)
        {
            Add(key, list);
        }
        #endregion

        #region 获取值列表
        /// <summary>
        /// 获取值列表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetParam(string key)
        {
            var data = Get(PublicKey.PARAMVALUECACHE_KEY) as Dictionary<string, Dictionary<string, string>>;
            if (data != null)
            {
                if (data.ContainsKey(key))
                {
                    var item = data[key] as Dictionary<string, string>;
                    if (item != null)
                        return item;
                }
            }
            return new Dictionary<string, string>();
        }
        #endregion

        #region 获取值列表值，根据Key,Code获取值
        /// <summary>
        /// 获取值列表值，根据Key,Code获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetParamValue(string key, string code)
        {
            var data = GetParam(key);
            if (!string.IsNullOrEmpty(code) && data.ContainsKey(code))
            {
                return data[code];
            }
            return "";
        }
        #endregion

    }
}
